/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaSesionesView.java
 *
 * Created on 24-ene-2011, 1:05:57
 */

package Vista.Sesion;

import Modelo.Entidades.Sesion;
import Modelo.Entidades.Usuario;
import Vista.Usuario.UsuarioView;
import java.util.ArrayList;
import java.util.List;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

public class ConsultaSesionesView extends javax.swing.JPanel {

    private UsuarioView usuarioView;
    private SesionView sesioneView;
    private SesionTableModel sesionTableModel;

    /** Creates new form ConsultaSesionesView */
    public ConsultaSesionesView(UsuarioView usuarioView, SesionView sesionView) {
        this.sesioneView = sesionView;
        this.usuarioView = usuarioView;
        this.sesionTableModel = SesionTableModel.create();

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelTablaSesiones = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablaSesiones = new javax.swing.JTable();
        jPanelCombo = new javax.swing.JPanel();
        jComboUsuario = new javax.swing.JComboBox();
        jPanelNumeroSesiones = new javax.swing.JPanel();
        jEtiquetaNumeroSesiones = new javax.swing.JLabel();

        jPanelTablaSesiones.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pantalla Sesiones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jTablaSesiones.setModel(sesionTableModel);
        jScrollPane1.setViewportView(jTablaSesiones);

        javax.swing.GroupLayout jPanelTablaSesionesLayout = new javax.swing.GroupLayout(jPanelTablaSesiones);
        jPanelTablaSesiones.setLayout(jPanelTablaSesionesLayout);
        jPanelTablaSesionesLayout.setHorizontalGroup(
            jPanelTablaSesionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTablaSesionesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTablaSesionesLayout.setVerticalGroup(
            jPanelTablaSesionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTablaSesionesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanelCombo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selección de Usuario", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jComboUsuario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jComboUsuarioMouseEntered(evt);
            }
        });

        javax.swing.GroupLayout jPanelComboLayout = new javax.swing.GroupLayout(jPanelCombo);
        jPanelCombo.setLayout(jPanelComboLayout);
        jPanelComboLayout.setHorizontalGroup(
            jPanelComboLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelComboLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboUsuario, 0, 528, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelComboLayout.setVerticalGroup(
            jPanelComboLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelComboLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelNumeroSesiones.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nº de Sesiones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jEtiquetaNumeroSesiones.setFont(new java.awt.Font("Tahoma", 3, 12));
        jEtiquetaNumeroSesiones.setForeground(java.awt.Color.darkGray);
        jEtiquetaNumeroSesiones.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jEtiquetaNumeroSesiones.setText("0");

        javax.swing.GroupLayout jPanelNumeroSesionesLayout = new javax.swing.GroupLayout(jPanelNumeroSesiones);
        jPanelNumeroSesiones.setLayout(jPanelNumeroSesionesLayout);
        jPanelNumeroSesionesLayout.setHorizontalGroup(
            jPanelNumeroSesionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNumeroSesionesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jEtiquetaNumeroSesiones, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelNumeroSesionesLayout.setVerticalGroup(
            jPanelNumeroSesionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNumeroSesionesLayout.createSequentialGroup()
                .addComponent(jEtiquetaNumeroSesiones, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanelNumeroSesiones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanelTablaSesiones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelTablaSesiones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanelNumeroSesiones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelCombo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(137, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboUsuarioMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboUsuarioMouseEntered
        List<Sesion> sesiones = new ArrayList();

        if(this.jComboUsuario.getSelectedItem().equals("Todos")){
            sesiones.addAll(this.sesioneView.getModel().listar());
            this.sesionTableModel.setSesiones(sesiones);
        }
        else{
            String dni = ((Usuario)this.jComboUsuario.getSelectedItem()).getDNI();
            sesiones.addAll(this.sesioneView.getModel().obtieneSesionesByDNI(dni));
            this.sesionTableModel.setSesiones(sesiones);
        }

        this.jEtiquetaNumeroSesiones.setText(String.valueOf(sesiones.size()));
    }//GEN-LAST:event_jComboUsuarioMouseEntered


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboUsuario;
    private javax.swing.JLabel jEtiquetaNumeroSesiones;
    private javax.swing.JPanel jPanelCombo;
    private javax.swing.JPanel jPanelNumeroSesiones;
    private javax.swing.JPanel jPanelTablaSesiones;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTablaSesiones;
    // End of variables declaration//GEN-END:variables

    public void refrescaVista() {
        List<Usuario> usuarios = this.usuarioView.getModel().listar();

        this.jComboUsuario.addItem("Todos");

        for(Usuario u: usuarios){
            this.jComboUsuario.addItem(u);
        }

        this.jComboUsuario.setSelectedItem("Todos");
    }

}

/*List<Sesion> sesiones = new ArrayList();

        if(this.jComboUsuario.getSelectedItem().equals("Todos")){
            sesiones.addAll(this.sesioneView.getModel().listar());
            this.sesionTableModel.setSesiones(sesiones);
        }
        else{
            String dni = ((Usuario)this.jComboUsuario.getSelectedItem()).getDNI();
            sesiones.addAll(this.sesioneView.getModel().obtieneSesionesByDNI(dni));
            this.sesionTableModel.setSesiones(sesiones);
        }

        this.jEtiquetaNumeroSesiones.setText(String.valueOf(sesiones.size()));*/
